
const app = getApp();
Page({
  data: {
    baseimgurl: 'https://wagoxcx.oss-cn-beijing.aliyuncs.com/index/',
    indicatorDots: false,
    kkkkkkk: false,
    showttttt:null,
    iconarr: [
      {
        img: 'sign_icon.png',
        text: '每日签到',
        url: '/pages/signIn/signIn'
      },
      {
        img: 'store_icon.png',
        text: '积分商城',
        url: "/pages/scoreStore/scoreStore"
      },
      {
        img: 'gift.png',
        text: '专属福利',
        url: "/pages/privilege/privilege"
      },
      {
        img: 'online_store.png',
        text: '积分规则',
        url: '/pages/ruleExplation/ruleExplation'
      },
      {
        img: 'ele_secure.png',
        text: '产品资讯',
        url: "/pages/safeEle/safeEle"
      },
      {
        img: 'invite_friend.png',
        text: '邀请好友',
        url: '/pages/invitefriends/invitefriends'
      },
      {
        img: 'sale_card.png',
        text: '销售名片',
        url: '/pages/contactUs/contactUs'
      },
      {
        img: 'ssstat.png',
        text: '关注我们',
        url: "/pages/gotoWeb/gotoWeb"
      },
    ],
    newsarr: [
      {
        img: '../../images/index/news1.png',
        text_top: '标题标题标题标题标题标题标题标题标题标题标题题题标题标题标题标题标题标题标题标题标题标题题标题标题标题标题标题标题标题标题标题标题题题标题标题标题标题标题标题标题标题标题标题题',
        text_bottom: '我是一串展示文字我是一串展示文字我是一串展示文字我是一串展示文字我是一串展示文字我是一串展示文字我是一串展示文字文我是一串展示文字我是一串展示文字文'
      },
      {
        img: '../../images/index/news2.png',
        text_top: '标题标题标题标题标题标题标题标题标题标题标题题题标题标题标题标题标题标题标题标题标题标题题标题标题标题标题标题标题标题标题标题标题题题标题标题标题标题标题标题标题标题标题标题题',
        text_bottom: '我是一串展示文字我是一串展示文字我是一串展示文字我是一串展示文字我是一串展示文字我是一串展示文字我是一串展示文字文我是一串展示文字我是一串展示文字文'
      },
      {
        img: '../../images/index/news3.png',
        text_top: '标题标题标题标题标题标题标题标题标题标题标题题题标题标题标题标题标题标题标题标题标题标题题标题标题标题标题标题标题标题标题标题标题题题标题标题标题标题标题标题标题标题标题标题题',
        text_bottom: '我是一串展示文字我是一串展示文字我是一串展示文字我是一串展示文字我是一串展示文字我是一串展示文字我是一串展示文字文我是一串展示文字我是一串展示文字文'
      }
    ],
    recommedarr: [

    ],
    carouselList: [

    ]
  },

  onLoad: function () {
    var value1 = wx.getStorageSync('openid');
    console.log(value1,999999999555555555555)
    if(value1){
      getApp().globalData.openid = value1
    }
    if(!getApp().globalData.openid){
          this.setData({
      showttttt:true
    })
    }

  },
  
  onShow: function () {
    this.getCarousel();
    this.getInfo();
    this.getnewproductList();
    this.getArticleList(null);
  },
  chossetoele: function () {
    wx.switchTab({
      url: '/pages/safeEle/safeEle',
    });
  },
  Topersonnnnn:function(){
    this.setData({
      showttttt:false
    })
    wx.switchTab({
      url: '/pages/personal/personal',
    });

  },
  chomeCarouselClick:function(e){
    var item = JSON.stringify(e.currentTarget.dataset.im);
    getApp().globalData.itemlunbotu = item;
   if(e.currentTarget.dataset.im.istz == 1 ){
  wx.navigateTo({
      url: '/pages/scanRule/scanRule'
    })
    }
  
  },
  closettttt:function(){
    this.setData({
      showttttt:false
    })
  },
  getCarousel: function () {
    var vm = this;
    wx.request({
      url: getApp().globalData.baseUrl + '/wago/lunbotu/select',
      method: 'post',
      header: {
        'Content-Type': 'application/x-www-form-urlencoded',
      },
      data: {
        cannel: 1
      },
      success: function (res) {
        vm.setData({
          carouselList: res.data.data
        })

      }
    })
  },
  //获取信息
  getInfo: function () {
    var vm = this;
    wx.request({
      url: getApp().globalData.baseUrl + '/wago/safety/selectByTime',
      method: 'post',
      success: function (res) {
        vm.setData({
          newInfoArr: res.data.data.slice(0, 5)
        })
      }
    })
  },
  // 新品列表
  getnewproductList() {
    var that = this
    wx.request({
      url: getApp().globalData.baseUrl + '/wago/product/selectnew',
      data: {
        currentPage: 1,
        pageSize: 4,
        queryString: ""
      },
      method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      // header: {}, // 设置请求的 header
      success: function (res) {
        // success
        console.log('res', res)
        if (res.data.total > 0) {
          // console.log('查新品',res.data.rows)
          that.setData({
            newproductList: res.data.rows,
            newproducttotal: res.data.total
          })
        }
      },
      fail: function () {
        // fail
      },
      complete: function () {
        // complete
      }
    })
  },
  // 跳转到取申请页面
  toapplication(event) {
    // console.log('chuzh',event.currentTarget.dataset.productid)
    wx.navigateTo({
      url: '/pages/application/application?productid=' + event.currentTarget.dataset.productid
    })
  },
  //查看文章详情
  toDetail: function (e) {
    var nowtypeid = this.data.nowtypeid
    //"&itemlist=" + JSON.stringify(this.data.Allrecommedarr)

    wx.navigateTo({
      url: "/pages/newsinfo2/newsinfo2?id=" + e.currentTarget.dataset.id
    })
  },
  //扫描二维码
  getScancode: function () {
    var userid = getApp().globalData.openid;
    var _this = this;
    // 允许从相机和相册扫码
    if (!userid) {
      wx.showToast({
        title: '请先登录',
        duration: 2000,
        icon: 'none'
      });
    } else {
      wx.scanCode({
        success: (res) => {
          var result = res.result;

          _this.setData({
            tresult: result,

          });
          _this.searchByResult()
        }
      })
    }


  },
  //根据扫描二维码查询
  searchByResult: function () {
    var vm = this;
    var userid = getApp().globalData.openid;
    if (!userid) {
      wx.showToast({
        title: '请先登录',
        duration: 2000,
        icon: 'none'
      });
    } else {
      wx.request({
        url: getApp().globalData.baseUrl + '/wago/saoma/saoma',
        method: 'post',
        data: {
          userid,
          qrc2: vm.data.tresult
        },
        success: function (res) {
          wx.showToast({
            title: res.data.message,
            duration: 2000,
            icon: 'none'
          });
        }
      })
    }
  },
  //根据分类拉取文章列表
  getArticleList: function (a) {
    var vm = this;
    var userid = getApp().globalData.openid;
    if (userid) {
      vm.setData({
        kkkkkkk: true
      })
      var typeid = a;
      var data = typeid ? {
        typeid,
        userid
      } :
        {
          userid
        }
      wx.request({
        url: getApp().globalData.baseUrl + '/wago/safety/selectByType',
        method: 'post',
        data,
        header: {
          'Content-Type': 'application/x-www-form-urlencoded',
        },
        success: function (res) {
          vm.setData({
            Allrecommedarr: res.data.data,
            recommedarr: res.data.data.slice(0, 10)
          })

        }
      })
    } else {
      vm.setData({
        kkkkkkk: false
      })
    }
  },
  onShareAppMessage: function () {
    let url = encodeURIComponent('/packageNews/pages/news_detail/news_detail?news_id=');
    return {
      title: "热点详情",
      path: `/pages/index/index`
    }

  },
  //根据url跳转
  toexact: function (e) {
    var url = e.currentTarget.dataset.url;

    if (e.currentTarget.dataset.url == "/pages/scoreStore/scoreStore" || e.currentTarget.dataset.url == "/pages/safeEle/safeEle") {
      wx.switchTab({
        url: e.currentTarget.dataset.url,
      });
    } else {
      if (e.currentTarget.dataset.url != "/pages/invitefriends/invitefriends" && e.currentTarget.dataset.url != "/pages/privilege/privilege") {
        if (e.currentTarget.dataset.url == "pages/shop/index/index.html?venderId=555341") {
          wx.navigateToMiniProgram({

            appId: 'wx91d27dbf599dff74',

            path: 'pages/shop/index/index?venderId=555341',

            envVersion: 'release',

            success(res) {

              // 打开成功

              console.log("跳转成功");

            }

          })
        } else {
          wx.navigateTo({
            url: e.currentTarget.dataset.url
          });
        }

      } else {
        if (getApp().globalData.openid) {
          wx.navigateTo({
            url: e.currentTarget.dataset.url
          });
        } else {
          wx.showToast({
            title: '登录后才能使用该功能',
            duration: 2000,
            icon: 'none'
          });
        }
      }

    }

  },
  //显示收藏
  showmodal: function (e) {
    this.clearCollect();
    //在data中保存当前的文章id
    this.setData({
      nowArticleid: e.currentTarget.dataset.articleid,
    });
    var vm = this;
    var oooid = getApp().globalData.openid;
    if (!oooid) {
      wx.showToast({
        title: '登录后才能使用该功能',
        duration: 2000,
        icon: 'none'
      });
    } else {
      wx.request({
        url: getApp().globalData.baseUrl + '/wago/wjj/selectByUserId',
        method: 'get',
        data: {
          userid: getApp().globalData.openid
        },
        success: function (res) {
          vm.setData({
            collectarr: res.data.data
          })
        }
      })
      this.setData({
        shownew: true
      })
    }
  },
  //关闭收藏
  onCloseNew: function () {
    this.setData({
      shownew: false
    })
    this.clearCollect();
  },
  //获取输入框的内容
  getInputContent: function (e) {
    this.setData({
      searchInput: e.detail.value
    })
  },
  //确认收藏
  submitfile: function () {
    var vm = this;
    var wjjId = this.data.nowCollerctId;
    var userid = getApp().globalData.openid;
    var shoucangId = this.data.nowArticleid;
    var nowiiiii = this.data.nowCollerctIndex;
    if (nowiiiii == 999882) {
      wx.showToast({
        title: '请选择收藏夹',
        duration: 2000,
        icon: 'none'
      });
    } else {
      wx.request({
        url: getApp().globalData.baseUrl + '/wago/shoucang/add',
        method: 'put',
        data: {
          userid,
          shoucangId,
          wjjId
        },
        success: function (res) {
          vm.setData({
            shownew: false,
            successcollect: true
          });
          setTimeout(() => {
            vm.setData({
              successcollect: false
            });
          }, 1000);
          vm.clearCollect();
        }
      })
    }
  },
  //清除收藏夹信息
  clearCollect: function () {
    this.setData({
      nowArticleid: undefined,
      nowCollerctIndex: 999882,
      nowCollerctId: undefined
    });
  },
  chooseCollect(e) {
    var id = e.currentTarget.dataset.idx;
    var index = e.currentTarget.dataset.index;
    this.setData({
      nowCollerctIndex: index,
      nowCollerctId: id
    })
  },
  //分享成功
  showsharemodal: function () {
    var vm = this;
    this.setData({
      successshare: true
    });
    setTimeout(() => {
      vm.setData({
        successshare: false
      });
    }, 1000);
  },


})
